﻿namespace Manga.Api.CCTuku
{
    using System;
    using System.Threading.Tasks;
    using Core;
    using Core.Models;
    using System.Text;

    public partial class CCGallery : ApiBase, IContentService
    {
        StringBuilder Builder = new StringBuilder();
        public string FavIcon { get => "http://www.tukucc.com/favicon.ico"; }
        public string HostUrl { get => "http://www.tukucc.com"; }
        public string Name { get => "CC图库"; }
        /// <summary>
        /// 指示当前网站查看漫画的时候能否直接确定它的最后一页,如果不能则为true
        /// </summary>
        public bool AsyncView { get => true; }

        private const string IndexNewsImageTemplate = @"//li[@class='k399']/dl/dt/img";
        private const string IndexNewsTitleTemplate = @"//li[@class='k399']/dl/dd/a";

        async public Task<IComicModel[]> News()
        {
            await Get(HostUrl);
            var imgs = Multi(IndexNewsImageTemplate);
            var Comics = new ComicModel[imgs.Count];
            for (int i = 0; i < imgs.Count; i++)
            {
                var a = Single(IndexNewsTitleTemplate);
                Comics[i] = new ComicModel()
                {
                    Description = string.Empty,
                    Thumbnail = imgs[i].Attributes["src"].Value,
                    Uri = HostUrl+a.Attributes["ref"].Value,
                    Title = a.InnerText
                };
            }
            return Comics;
        }

        private const string IndexHotTemplate = @"/html/body/div[11]/div[1]/div[2]/div";
        private const string IndexRecommendTemplate = @"/html/body/div[12]/div[1]/div[2]/div";
        //10
        async public Task<IComicModel[]> Hot()
        {
            
            try
            {
                if (!Equal(HostUrl)) await Get(HostUrl);
                var nodes = Multi(IndexHotTemplate);
                var comics = new ComicModel[nodes.Count];
                for (int i = 0; i < comics.Length; i++)
                {
                    var a = Single(nodes[i], ".//div[1]/div[1]/a");
                    comics[i] = new ComicModel()
                    {
                        Thumbnail = Single(a, ".//img").Attributes["src"].Value,
                        Uri = HostUrl + a.Attributes["href"].Value,
                        Title = Single(nodes[i], ".//p/a").InnerText,
                        Description = Single(nodes[i], ".//p/a").InnerText
                    };
                }
                return comics;
            }
            catch
            {
                return null;
            }
        }

        //11
        async public Task<IComicModel[]> Recommend()
        {
            try
            {
                if (!Equal(HostUrl)) await Get(HostUrl);
                var nodes = Multi(IndexRecommendTemplate);
                var comics = new ComicModel[nodes.Count];
                for (int i = 0; i < comics.Length; i++)
                {
                    var a = Single(nodes[i], ".//div[1]/div[1]/a");
                    comics[i] = new ComicModel()
                    {
                        Thumbnail = Single(a, ".//img").Attributes["src"].Value,
                        Uri = HostUrl + a.Attributes["href"].Value,
                        Title = Single(nodes[i], ".//p/a").InnerText,
                        Description = Single(nodes[i], ".//p/a").InnerText
                    };
                }
                return comics;
            }
            catch
            {
                return null;
            }
        }
    }
}
